const UserModel = require("../../models/UserModel")

const UserService = {
    login:({username,password})=>{
        return UserModel.find({
            username,password
        },{password:0})
    },
    detail:(_id)=>{
        return UserModel.find({
            _id
        })
    },
    update:({_id,...userInfo})=>{
        return UserModel.updateOne({_id},userInfo)
    },
    add:(userInfo)=>{
        return UserModel.create(userInfo)
    },
    list:async ({pageSize,currentPage})=>{
        const data = await UserModel.find({},{
            password:0
        }).skip((currentPage-1)*pageSize).limit(pageSize)
        const total=await UserModel.countDocuments()
        return {
            data,
            total
        }
    },
    delete:(_id)=>{
        return UserModel.deleteOne({
            _id
        })
    }
}

module.exports = UserService